package cn.itcast.travel.web.servlet;

import cn.itcast.travel.domain.PageBean;
import cn.itcast.travel.domain.Route;
import cn.itcast.travel.domain.User;
import cn.itcast.travel.service.FavoriteService;
import cn.itcast.travel.service.RouteService;
import cn.itcast.travel.service.impl.FavoriteServiceImpl;
import cn.itcast.travel.service.impl.RouteServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/route/*")
public class RouteServlet extends BaseServlet {

    private RouteService routeService = new RouteServiceImpl();
    private FavoriteService favoriteService = new FavoriteServiceImpl();

    public void pageQuery(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.接收参数
        String cidStr = request.getParameter("cid");
        System.out.println(cidStr);
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");

        //接收rname线路名称
        String rname = request.getParameter("rname");
        //åµé©¬ä¿ 乱码
        //System.out.println(rname);
        rname = new String(rname.getBytes("iso-8859-1"), "utf-8");
        System.out.println(rname);
        //2.处理参数
        int cid = 0;
        if (cidStr != null && cidStr.length() > 0 && !"null".equals(cidStr)) {
            cid = Integer.parseInt(cidStr);
        }

        int currentPage = 0;
        if (currentPageStr != null && currentPageStr.length() > 0) {
            currentPage = Integer.parseInt(currentPageStr);
        } else {
            currentPage = 1;
        }
        int pageSize = 0;
        if (pageSizeStr != null && pageSizeStr.length() > 0) {
            pageSize = Integer.parseInt(pageSizeStr);
        } else {
            pageSize = 5;
        }
        //3.调用service查询PageBean对象
        PageBean<Route> pb = routeService.pageQuery(cid, currentPage, pageSize, rname);
        //4.将pageBean对象序列化为json,返回

        writeValue(pb, response);


    }

    public void findOne(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.接收rid
        String rid = request.getParameter("rid");
        //2.调用Service查询route对象
        Route route = routeService.findOne(rid);
        //3.转为json写回客户端
        writeValue(route, response);
    }

    /**
     * 判断当前登录用户是否收藏过该路线
     *
     * @param request
     * @param response
     * @throws Exception
     */
    public void isFavorite(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.获取路线id
        String rid = request.getParameter("rid");
        //2.获取当前登录的用户 user
        User user = (User) request.getSession().getAttribute("user");
        int uid;//定义用户id
        if (user == null) {
            //用户尚未登录
            uid = 0;
        } else {
            //用户已经登录
            uid = user.getUid();
        }
        //3.调用
        boolean flag = favoriteService.isFavorite(rid, uid);
        //4.写回客户端
        writeValue(flag, response);
    }

    /**
     * @param request
     * @param response
     * @throws Exception
     */
    public void addFavorite(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.获取线路rid
        String rid = request.getParameter("rid");
        //2. 获取当前登录的用户
        User user = (User) request.getSession().getAttribute("user");
        int uid;//用户id
        if (user == null) {
            //用户尚未登录
            return;
        } else {
            //用户已经登录
            uid = user.getUid();
        }


        //3. 调用service添加
        favoriteService.add(rid, uid);


    }


}
